package com.cloudkinto.service.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.service.consumer.vo.orderSyncRecord.PlatformOrderSyncRecordReq;
import com.cloudkinto.service.consumer.vo.rakuten.ItemUpdateConsumerReq;
import com.cloudkinto.service.consumer.vo.rakuten.RakutenOrderUpdateConReq;
import com.cloudkinto.service.consumer.vo.shopify.ShopifyConsumerReq;
import com.cloudkinto.service.consumer.vo.shopify.WowmaConsumerReq;
import com.cloudkinto.service.consumer.vo.wowma.WowmaMarkShipConsumerReq;
import com.cloudkinto.service.consumer.vo.wowma.WowmaMarkStatusConsumer;
import com.cloudkinto.service.consumer.vo.yahoo.YahooConsumerReq;
import com.cloudkinto.service.consumer.vo.yahoo.YahooMarkShipConsumerReq;
import com.cloudkinto.service.platform.PlatformOrderWowmaSyncService;
import com.cloudkinto.service.platform.PlatformOrderYahooSyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformOrderWowmaConsumer {
    private final PlatformOrderWowmaSyncService platformOrderWowmaSyncService;
    private final RabbitTemplate rabbitTemplate;


    /**
     * 同步商品 获取itemCode
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.GetPlatformProduct_Wowma))
    public void searchItemInfos(String msg) {
        ItemUpdateConsumerReq req = JSONObject.parseObject(msg, ItemUpdateConsumerReq.class);
        try {
            platformOrderWowmaSyncService.searchItemInfos(req.getStoreDo(), req.getParentSku());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 同步订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Wowma_Sync))
    public void syncOrderRakuten(String msg) {
        WowmaConsumerReq req = JSONObject.parseObject(msg, WowmaConsumerReq.class);
        String s = null;
        try {
            s = platformOrderWowmaSyncService.updatWowmaOrders(req);
        } catch (Exception e) {
            s = e.getMessage();
        }
        if (StringUtils.isNotBlank(s)) {
            rabbitTemplate.convertAndSend(RabbitQueueConstant.PlatformOrder_SyncRecord_Update, JSON.toJSONString(new PlatformOrderSyncRecordReq(req.getSyncId(), 3, new Date(), null, s, null)));
        }
    }


    /**
     * 定时同步未完成的订单
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Wowma_Update_Sync))
    public void updateLocal(String msg) {
        try {
            platformOrderWowmaSyncService.updateLocalOrders(Long.parseLong(msg));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 更新wowma订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Wowma_Update))
    public void syncOrderRakutenUpdate(String msg) {
        log.info(msg);
        RakutenOrderUpdateConReq req = JSONObject.parseObject(msg, RakutenOrderUpdateConReq.class);
        try {
            platformOrderWowmaSyncService.consumerUpdate(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 标发
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Wowma_MarkShip))
    public void markShip(String msg) {
        log.info(msg);
        WowmaMarkShipConsumerReq req = JSONObject.parseObject(msg, WowmaMarkShipConsumerReq.class);
        try {
            platformOrderWowmaSyncService.makeShip(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 更改订单状态
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Wowma_MarkStatus))
    public void markStatus(String msg) {
        log.info(msg);
        WowmaMarkStatusConsumer req = JSONObject.parseObject(msg, WowmaMarkStatusConsumer.class);
        try {
            platformOrderWowmaSyncService.makeStatus(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
